package org.example.currency;

import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

/**
 * @Title : S3
 * @Description:
 * @author: libo@juzishuke.com
 * @date: 2023/12/4 16:08
 * @Version:1.0
 */
public class SemaphoreExample3 {

    public static void main(String[] args) throws InterruptedException {
        final Semaphore semaphore = new Semaphore(1, true);
        // 创建线程t1
        Thread t1 = new Thread(() -> {
            try {
                // 获取Semaphore的许可证
                semaphore.acquire();
                System.out.println("The thread t1 acquired permit from semaphore.");
                // 霸占许可证一个小时
                TimeUnit.HOURS.sleep(1);
            } catch (InterruptedException e) {
                System.out.println("The thread t1 is interrupted");
            } finally {
                // 在finally语句块中释放许可证
                System.out.println("The thread t1 release permit to semaphore."+semaphore.availablePermits());
                semaphore.release();
            }
        });
        // 启动线程t1
        t1.start();
        // 为确保线程t1已经启动，在主线程中休眠1秒稍作等待
        TimeUnit.SECONDS.sleep(1);
        // 创建线程t2
        Thread t2 = new Thread(() -> {
            try {
                // 阻塞式地获取一个许可证
                semaphore.acquire();
                System.out.println("The thread t2 acquired permit from semaphore.");
            } catch (InterruptedException e) {
                System.out.println("The thread t2 is interrupted");
            } finally {
                // 同样在finally语句块中释放已经获取的许可证
                System.out.println("The thread t2 release permit to semaphore."+semaphore.availablePermits());
                semaphore.release();
            }
        });
        // 启动线程t2
        t2.start();
        // 休眠2秒后
        TimeUnit.SECONDS.sleep(2);
        // 对线程t2执行中断操作
        t2.interrupt();
        // 主线程获取许可证
        semaphore.acquire();
        System.out.println("The main thread acquired permit.");
        t1.interrupt();
    }
}
